package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.common.mapper.ChuanYinMapper;
import com.tanhua.common.pojo.ChuanYin;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.pojo.UserThreadLocal;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.server.api.ChuanYinApi;
import com.tanhua.server.vo.ChuanYinVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ChuanYinService {
//    @Autowired
//    private PicUploadService picUploadService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ChuanYinMapper chuanYinMapper;


    @Autowired
    private FastFileStorageClient storageClient;
    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Reference(version = "1.0.0")
    private ChuanYinApi chuanYinApi;


    public ChuanYinVo AcceptMessage() {
        User user = UserThreadLocal.get();
        //查询剩余次数
        Integer count = chuanYinApi.queryCount(user.getId());

        QueryWrapper<ChuanYin> queryWrapper = new QueryWrapper<>();
        List<ChuanYin> chuanYins = chuanYinMapper.selectList(queryWrapper);
        //生成随机对象
        ChuanYin chuanYin = new ChuanYin();
        while (true) {
            chuanYin = RandomUtil.randomEle(chuanYins);
            //规避自己
            if (chuanYin.getUserId() != user.getId()) {
                break;
            }
            //todo 规避已经喜欢的人 chuanYin的Id和redis中对比

        }
        //删除接收后数据
        chuanYinMapper.deleteById(chuanYin.getId());
        storageClient.deleteFile(chuanYin.getSoundUrl());
        //封装返回对象
        ChuanYinVo chuanYinVo = new ChuanYinVo();
        chuanYinVo.setRemainingTimes(count);
        chuanYinVo.setNickname(chuanYin.getNickname());
        chuanYinVo.setGender(chuanYin.getSex().name().toLowerCase());
        chuanYinVo.setAvatar(chuanYin.getAvatar());
        chuanYinVo.setId(Convert.toInt(chuanYin.getUserId()));
        chuanYinVo.setAge(chuanYin.getAge());
        chuanYinVo.setSoundUrl(chuanYin.getSoundUrl());
        return chuanYinVo;
    }

    public void SendMessage(MultipartFile soundFile)  {
//fastdfs存语音
        try {
            StorePath storePath = storageClient.uploadFile(soundFile.getInputStream(),
                    soundFile.getSize(),
                    StrUtil.subAfter(soundFile.getOriginalFilename(), '.', true),
                    null);
            //PicUploadResult result = picUploadService.upload(soundFile);
            User user = UserThreadLocal.get();
            UserInfo userInfo = userInfoService.queryUserInfoByUserId(user.getId());
            if (ObjectUtil.isEmpty(userInfo)) {
                return;
            }
            ChuanYin chuanYin = new ChuanYin();
            chuanYin.setUserId(userInfo.getUserId());
            chuanYin.setAvatar(userInfo.getLogo());
            chuanYin.setAge(userInfo.getAge());
            chuanYin.setSex(userInfo.getSex());
            chuanYin.setCreated(new Date());
            chuanYin.setUpdated(chuanYin.getCreated());
            chuanYin.setNickname(userInfo.getNickName());
            chuanYin.setSoundUrl(fdfsWebServer.getWebServerUrl() + storePath.getFullPath());
            chuanYinMapper.insert(chuanYin);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
